﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace Lab6
{
    public enum DrawMode { dmSelect, dmRect, dmPolygon, dmLine };

    public partial class GraphicEditor : Form
    {
        string fileName = "";
        Graphics g;
        Image image;
        Pen myPen = new Pen(Color.CadetBlue, 3);
        Boolean drawMode;
        Boolean shouldPaint;
        Boolean shouldDraw;
        int drawIndex;
        Point first;
        Point last;
        DrawMode curDrawMode = DrawMode.dmRect;
        List<Child> children = new List<Child>();
        Child child;

        public GraphicEditor()
        {
            InitializeComponent();

        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImageDialog dialog = new ImageDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Child child = new Child(dialog.newImageWidth, dialog.newImageHeight);
                child.Text = "New Image";
                child.MdiParent = this;
               
                child.Show();
                saveToolStripMenuItem.Enabled = true;
                saveAsToolStripMenuItem.Enabled = true;
                children.Add(child);
            }

        }
        private void imageToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            g = this.CreateGraphics();
            this.BackColor = Color.Turquoise;
            loadImageToolStripMenuItem.Enabled = true;
            g.Dispose();
        }

        //open an image from a file        
        private void openFromFileToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                fileName = openFileDialog1.FileName;
                image = Image.FromFile(fileName);
              //  Child child = new Child(image, fileName);
                child = new Child(image, fileName);
                child.Text = fileName;
                child.MdiParent = this;

                child.Show();
            }

            saveToolStripMenuItem.Enabled = true;
            saveAsToolStripMenuItem.Enabled = true;
        }
        //open an image from web
        private void openFromWebToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebDialog dialog = new WebDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {

                try
                {
                    string url = dialog.FileName;
                    WebRequest requestPic = WebRequest.Create(url);
                    requestPic.Method = "GET";
                    WebResponse responsePic = requestPic.GetResponse();
                    image = Image.FromStream(responsePic.GetResponseStream());
                    Child child = new Child(image, "");
                    child.Text = "New Image";
                    child.MdiParent = this;

                    child.Show();
                    g = this.CreateGraphics();

                    g.Clear(this.BackColor);
                    g.DrawImage(image, 0, 0, image.Width, image.Height);
                    g.Dispose();
                }
                catch (Exception exception)
                {
                    MessageBox.Show("web exception: " + exception.Message,
                          Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }


        //save an image
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif | All files|*.* ";
            //fileName = saveFileDialog1.FileName;
            //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            
                if (fileName != string.Empty)
                {
                    // Saves the Image via a FileStream created by the OpenFile method.
                      //  System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();

                    switch (saveFileDialog1.FilterIndex)
                    {
                        case 1:
                            this.image.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            break;

                        case 2:
                            this.image.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
                            break;

                        case 3:
                            this.image.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
                            break;
                    }
                    //   fs.Close();
                }
                else
                {
                    saveAsToolStripMenuItem_Click(sender, e);
                }
            
        }
        //close the form when exit is clicked
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif | All files|*.* ";
            
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = saveFileDialog1.FileName;
                MessageBox.Show(fileName);
              
                if (fileName != string.Empty)
                {
                    // Saves the Image via a FileStream created by the OpenFile method.
                  //  System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();
                 
                    switch (saveFileDialog1.FilterIndex)
                    {
                        case 1:
                            child.childImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            break;
                            
                        case 2:
                            child.childImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
                            break;

                        case 3:
                            child.childImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
                            break;
                    }
                   // fs.Close();
                }
            }
            if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
        }

        private void freeHandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            drawMode = false;
            toolStripStatusLabel1.Text = "Free Hand";
        }

        //set drawMode, drawIndex, and status label for drawing line
        private void lineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            drawMode = true;
            toolStripStatusLabel1.Text = "Line";
            drawIndex = 1;

        }

        //set drawMode, drawIndex, and status label for drawing rectangle
        private void rectangleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (children.Count > 0)
                children[0].drawMode = DrawMode.dmRect;
            drawMode = true;
            toolStripStatusLabel1.Text = "Rectangle";
            drawIndex = 2;
        }

        //set drawMode, drawIndex, and status label for drawing ellipse
        private void ellipseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            drawMode = true;
            toolStripStatusLabel1.Text = "Ellipse";
            drawIndex = 3;
        }
        //set pen color to black
        private void blackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.myPen = new Pen(Color.Black, 3);
        }

        //set pen color to the desired color picked from color dialog box
        private void colourToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.myPen = new Pen(colorDialog1.Color, 3);
            }
        }

        private void GraphicEditor_MouseClick(object sender, MouseEventArgs e)
        {
            int count = 1;

            if (drawMode)
            {
                if (!shouldPaint)
                {
                    // get the point from the first click
                    first.X = e.X;
                    first.Y = e.Y;

                    //once we get the first point, we are ready to draw
                    shouldPaint = true;
                }
                else
                {
                    //refresh window to clear the previous drawing
                    this.Refresh();

                    //create a graphic object
                    g = CreateGraphics();

                    //get the point from the second click
                    last.X = e.X;
                    last.Y = e.Y;

                    //set x, y coordinate
                    int x = first.X;
                    int y = first.Y;

                    //set the width and height of shape
                    int width = Math.Abs(first.X - last.X);
                    int height = Math.Abs(first.Y - last.Y);

                    //set the x, y coordinate to upper left corner position
                    if (last.X < first.X && last.Y < first.Y)
                    {
                        x = last.X;
                        y = last.Y;
                    }
                    else if (last.X < first.X && last.Y > first.Y)
                    {
                        x = last.X;
                        y = first.Y;
                    }
                    else if (last.X > first.X && last.Y < first.Y)
                    {
                        x = first.X;
                        y = last.Y;
                    }


                    switch (drawIndex)
                    {
                        //draw line         
                        case 1: g.DrawLine(this.myPen, first, last);
                            break;
                        //draw rectangle
                        case 2: if (width > 0 && height > 0)
                                g.DrawRectangle(myPen, x, y, width, height);
                            break;
                        //draw ellipse
                        case 3:
                            if (width > 0 && height > 0)
                                g.DrawEllipse(myPen, x, y, width, height);
                            break;
                        default:
                            break;
                    }

                    g.Dispose();
                    ++count;        //increment drawings
                }
                //set drawing mode to false when finish drawing line/rectange/ellipse so that the
                //next drawing will be positioned in the right place when mouse is clicked
                if (count % 2 == 0)
                {
                    shouldPaint = false;
                }

            }
        }

        private void GraphicEditor_MouseMove(object sender, MouseEventArgs e)
        {
            if (!drawMode)
            {
                if (shouldDraw)
                {
                    this.Refresh();
                    g = CreateGraphics();
                    g.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
                    g.Dispose();
                }
            }
        }

        private void GraphicEditor_MouseDown(object sender, MouseEventArgs e)
        {

            shouldDraw = true;
        }

        private void GraphicEditor_MouseUp(object sender, MouseEventArgs e)
        {
            shouldDraw = false;
        }

        private void centreImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            int wid = (this.Width - image.Width) / 2;
            int hght = (this.Height - image.Height) / 2;
            g = this.CreateGraphics();
            g.Clear(this.BackColor);
            g.DrawImage(image, wid, hght, image.Width, image.Height);
            g.Dispose();
        }

        private void scaleToRectangleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            g = this.CreateGraphics();
            g.Clear(this.BackColor);
            g.DrawImage(image, 0, 0, this.Width, this.Height);
            g.Dispose();

        }

        private void reflectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;

            int x = this.Width / 2;
            int y = this.Height / 2;

            g = this.CreateGraphics();
            g.Clear(this.BackColor);

            g.DrawImage(image, x, y, -image.Width, -image.Height);
            g.DrawImage(image, x, y, image.Width, -image.Height);
            g.DrawImage(image, x, y, -image.Width, image.Height);
            g.DrawImage(image, x, y, image.Width, image.Height);

            g.Dispose();
        }

        private void randomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            g = this.CreateGraphics();
            g.Clear(this.BackColor);
            Random random = new Random();
            int x = random.Next(this.Width);
            int y = random.Next(this.Height);
            int x1 = random.Next(this.Width);
            int y1 = random.Next(this.Height);

            g.DrawImage(image, x, y, x1, y1);
            g.Dispose();
        }

        private void rotateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;

            Point A = new Point(this.Width / 2, 0);
            Point B = new Point(this.Width, this.Height / 2);
            Point C = new Point(0, this.Height / 2);
            Point[] p3Fro = { A, B, C };

            g = this.CreateGraphics();
            g.Clear(this.BackColor);
            g.DrawImage(image, p3Fro);

            g.Dispose();
        }



        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Tick += new EventHandler(randomToolStripMenuItem_Click);
            timer1.Interval = 1;
        }

        private void selectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (children.Count > 0)
                children[0].drawMode = DrawMode.dmSelect;
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (children.Count > 0)
                children[0].CopyShape();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (children.Count > 0)
                children[0].PasteShape();
        }

        private void polygonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (children.Count > 0)
                children[0].drawMode = DrawMode.dmPolygon;
        }






    }
}
